package com.shanpin.shebao.admin.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.shanpin.core.utils.UUIDGenerator;
import com.shanpin.shebao.admin.exception.ParamParserException;
import com.shanpin.shebao.common.URI;
import com.shanpin.shebao.constant.QueryOperation;
import com.shanpin.shebao.form.LinkQueryForm;
import com.shanpin.shebao.model.Link;
import com.shanpin.shebao.service.LinkService;
import com.shanpin.shebao.utility.Criterition;
import com.shanpin.shebao.utility.MoreCriteria;
import com.shanpin.shebao.utility.PageModel;

/**
 * 链接管理控制器
 * @author 品讯科技
 *
 */
@Controller
public class LinkAdminController extends ResourceController {
	
	@Autowired
	private LinkService linkService;
	
	@Autowired
	private UUIDGenerator uuidGenerator;
	
	/**
	 * 链接管理查询列表
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 */
	@RequestMapping(value=URI.COUPON_LIST_LINK, method=RequestMethod.GET)
	public Object queryPageBy(HttpServletRequest request, HttpServletResponse response, LinkQueryForm form) {
		
		MoreCriteria<String, Criterition> mc = new MoreCriteria<String, Criterition>();
		Map<String, Criterition> pair = (Map<String, Criterition>) mc.getPair();
		if(form.getUrl() != null && !"".equals(form.getUrl())) {
			Criterition c = new Criterition("link.url", form.getUrl().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("url", c);
		}
		
		if(form.getCode() != null && !"".equals(form.getCode())) {
			Criterition c = new Criterition("link.code", form.getCode().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("code", c);
		}
		
		if(form.getShare_id() != null && !"".equals(form.getShare_id())) {
			Criterition c = new Criterition("link.share_id", form.getShare_id().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("share_id", c);
		}
		
		if(form.getEnd_date() != null && !"".equals(form.getEnd_date())) {
			Criterition c = new Criterition("link.end_date", form.getEnd_date().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("end_date", c);
		}
		
		if(form.getType() != null && !"".equals(form.getType())) {
			Criterition c = new Criterition("link.type", form.getType().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("type", c);
		}
		
		if(form.getPurpose() != null && !"".equals(form.getPurpose())) {
			Criterition c = new Criterition("link.purpose", form.getPurpose().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("purpose", c);
		}
		
		if(form.getMonth_limit() != null && !"".equals(form.getMonth_limit())) {
			Criterition c = new Criterition("link.month_limit", form.getMonth_limit().trim(), QueryOperation.EQ, QueryOperation.AND);
			pair.put("month_limit", c);
		}
		
		int pageSize = PAGE_SIZE;
		
		int page = parsePageNo(form.getPageNo());
		
		PageModel pageModel = linkService.queryPage(page, pageSize, mc);
		request.setAttribute("pageModel", pageModel);
		request.setAttribute("linkQueryForm", form);
		
		request.setAttribute("coupon_list_uri", URI.SBA + URI.COUPON_LIST);	
		request.setAttribute("coupon_channel_list_uri", URI.SBA + URI.COUPON_LIST_CHANNEL);	
		request.setAttribute("coupon_link_list_uri", URI.SBA + URI.COUPON_LIST_LINK);
		request.setAttribute("new_link_uri", URI.SBA + URI.COUPON_NEW_LINK);
		
		request.setAttribute("coupon_channel", false);
		request.setAttribute("coupon", false);
		request.setAttribute("coupon_link", true);
		return "coupon_manage";
	}
	
	/**
	 * 添加链接
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 */
	@RequestMapping(value=URI.COUPON_NEW_LINK, method=RequestMethod.POST)
	public Object add(HttpServletRequest request, HttpServletResponse response, LinkQueryForm form) {
		
		if(form != null) {
			
			checkForm(form);
			
			Link link = new Link();
			link.setUrl(form.getUrl());
			
			link.setType(Integer.parseInt(form.getType()));
			
			link.setPurpose(Integer.parseInt(form.getPurpose()));
			
			link.setMonth_limit(Integer.parseInt(form.getMonth_limit()));
			
			link.setCode(uuidGenerator.generate().toString());
			
			if(form.getEnd_date() != null && !"".equals(form.getEnd_date())) {
				try {
					link.setEnd_date(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(form.getEnd_date() + " 23:59:59"));
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
			
			link.setComment(form.getComment());
			
			linkService.add(link);
			
		}
		
		return "redirect:"+URI.SBA+URI.COUPON_LIST_LINK+"?pageNo=1";
	}

	private void checkForm(LinkQueryForm form) {
		
		if(form.getUrl() == null || "".equals(form.getUrl())) {
			throw new ParamParserException("链接URL不能为空");
		}
		
		if(form.getMonth_limit() == null || "".equals(form.getMonth_limit())) {
			form.setMonth_limit("0");
		}
		
		if(form.getType() == null || "".equals(form.getType())) {
			throw new ParamParserException("链接类型不能为空");
		}
		
		if(form.getPurpose() == null || "".equals(form.getPurpose())) {
			throw new ParamParserException("链接用途不能为空");
		}
		
	}
	
}
